package com.example.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.contant.MessageConstant;
import com.example.entity.PageResult;
import com.example.entity.QueryPageBean;
import com.example.entity.Result;
import com.example.pojo.Goods;
import com.example.service.GoodsAdminService;
import com.example.utils.QiniuUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/goods")
@RestController
public class GoodsController {


    @Reference
    private GoodsAdminService goodsAdminService;

    @PostMapping("findpage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        System.out.println(queryPageBean.getPageSize());
            return goodsAdminService.findpage(queryPageBean);
    }
    @RequestMapping ("upload")
    public Result upload(MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();//原始名字
        int i = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(i);
        String prefix= UUID.randomUUID().toString();
        String filename=prefix+suffix;
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),filename);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,filename);
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Goods goods, Integer[] cid){
        goods.setCId(Integer.parseInt(cid[0].toString()));
        try{
            goodsAdminService.add(goods);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.ADD_GOODS_FAIL);
        }
        return new Result(true,MessageConstant.ADD_GOODS_SUCCESS);
    }
    @GetMapping("/delete")
    public Result delete(Integer id){

        try{
            goodsAdminService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.DELETE_GOODS_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_GOODS_SUCCESS);
    }
    @GetMapping("/findById")
    public Result findById(Integer id){
       try{
           Goods goods=goodsAdminService.findById(id);
           System.out.println(goods.getCId());
           return  new Result(true,MessageConstant.QUERY_GOODS_SUCCESS,goods);
       }catch (Exception e){
           e.printStackTrace();
           return new Result(false,MessageConstant.QUERY_GOODS_FAIL);
       }
    }
    @PostMapping("/edit")
    public Result edit(@RequestBody Goods goods, Integer[] cid){
        goods.setCId(Integer.parseInt(cid[0].toString()));
        try{
            goodsAdminService.edit(goods);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_GOODS_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_GOODS_SUCCESS);

    }

}
